package me.sealer.algorithm.utils.string;

/**
 * Created by sealer on 17/04/16.
 */
public class StringMatch {
    public static int match(String target, String pattern) {
        if (isNull(target) || isNull(pattern)) {
            return -1;
        }

        int tLength = target.length();
        int pLength = pattern.length();

        int i = 0, j = 0;//i target的index,  j pattern的index
        while (i < tLength && j < pLength) {
            if (target.charAt(i) == pattern.charAt(j)) {
                i++;
                j++;
            } else {
                if (j == 0) {
                    i++;
                } else {
                    j = 0;
                }

            }
        }

        if (i < tLength) {
            return i - pLength;
        } else {
            return -1;
        }

    }


    public static boolean isNull(String s) {
        return s == null || "".equals(s);
    }
}
